package com.wan.tool;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

public class Word {

    public int num;
    public String word;
    public String chinese;
    public String yin;
    public int showStatus;//0 未展示，1 正在展示 ， 2 展示完成
    public NetWord netWord;

    public Word(int num, String word, String yin, String chinese) {
        this.num = num;
        this.word = word;
        this.yin = yin;
        this.chinese = chinese;
    }


    //不会返回null，顶多会返回size＝＝0
    public static List<Word> getWordList(int start, int count, int taskId) {
        List<Word> wordList = new ArrayList<Word>();
        SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(DBManage.DB_PATH + "/" + DBManage.DB_NAME, null);
        String sql = "select word.id , word.word,word.description ,word.phonetic  from word , book2word  where word.id=book2word.wordId and book2word.bookId = " + taskId + " order by orderNo limit " + start + " , " + count;
        Cursor cursor = database.rawQuery(sql, null);
        if (cursor != null) {
            cursor.moveToFirst();
            while (!cursor.isAfterLast()) {
                int num = cursor.getInt(cursor.getColumnIndex("id"));
                String yin = cursor.getString(cursor.getColumnIndex("phonetic"));
                String wordEn = cursor.getString(cursor.getColumnIndex("word"));
                String chinese = cursor.getString(cursor.getColumnIndex("description"));
                if (chinese != null) {
                    String[] chineses = chinese.split(";");
                    chinese = "";
                    for (int i = 0; i < chineses.length; i++) {
                        if (i >= 5) {
                            break;
                        }
                        chinese += chineses[i] + "   ";
                    }
                }
                Word word = new Word(num, wordEn, yin, chinese);
                wordList.add(word);
                cursor.moveToNext();
            }
            cursor.close();
        }
        database.close();
        return wordList;
    }

    public static Word getOneRandomWord() {
        SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(DBManage.DB_PATH + "/" + DBManage.DB_NAME, null);
        String sql = "select id , phonetic ,word ,description from word order by random() limit 1";
        Word word = null;
        Cursor cursor = database.rawQuery(sql, null);
        if (cursor != null) {
            cursor.moveToFirst();
            while (!cursor.isAfterLast()) {
                int num = cursor.getInt(cursor.getColumnIndex("id"));
                String yin = cursor.getString(cursor.getColumnIndex("phonetic"));
                String wordEn = cursor.getString(cursor.getColumnIndex("word"));
                String chinese = cursor.getString(cursor.getColumnIndex("description"));
                if (yin == null) {
                    yin = "";
                }
                word = new Word(num, wordEn, yin, chinese);
                cursor.moveToNext();
            }
            cursor.close();
        }
        database.close();
        return word;
    }

}
